<?php
class Solvex_Locale
{
	const DEFAULT_LOCALE 		= 'en_US';
	const DEFAULT_CURRENCY		= 'USD';
	const DEFAULT_TIMEZONE		= 'America/New_York';
	
	public static function setLocale($locale = 'auto')
	{
		// check if locale is registered and set to $locale parameter
		if(Zend_Registry::isRegistered('Zend_Locale')){
			$currentLocale = Zend_Registry::get('Zend_Locale');
			if($locale === $currentLocale->toString()){
				return;
			}
		}
		
		// setup locale for installer
		if(Solvex_Area::isInstaller()){
			$session = Solvex::session('install');
			
			if(Zend_Registry::isRegistered('Zend_Locale')){
				$currentLocale = Zend_Registry::get('Zend_Locale');
			} else {
				$currentLocale = new Zend_Locale();
				Zend_Locale::setCache(Solvex::cache());
				Zend_Registry::set('Zend_Locale', $currentLocale);
			}
			
			// check if $locale param is a valid locale
			if($currentLocale->isLocale($locale)){
				$currentLocale->setLocale($locale);
			}
			
			$session->current_locale = $locale;
			return;
		}
		
		$session = Solvex::session();
		
		// fix locale if not in en_US format. if not existent, uses default locale
		if(!stristr($locale, '_')){
			$locale = self::_getLocaleFromLanguageCode($locale);
		}
		
		// setup locale
		if(Zend_Registry::isRegistered('Zend_Locale')){
			$currentLocale = Zend_Registry::get('Zend_Locale');
			$currentLocale->setLocale($locale);
		} else {
			try {
				$currentLocale = new Zend_Locale($locale);
			} catch(Zend_Locale_Exception $e){
				$currentLocale = new Zend_Locale(self::DEFAULT_LOCALE);
			}
			Zend_Locale::setCache(Solvex::cache());
			Zend_Registry::set('Zend_Locale', $currentLocale);
		}
		
		$availableLanguages = Solvex::table('language')
			->select(array('locale','id'))
			->fetchPairs();

		if(Solvex_Area::isBackend()){
			$session->locale = $locale;
			$defaultLanguage = Solvex::config('locale/main/language_admin');
			if(array_search($defaultLanguage, $availableLanguages)){
				$session->language = $defaultLanguage;
			} else {
				$session->language = current($availableLanguages);
			}
		} else {
			$localeCode = $currentLocale->toString();
			if(isset($availableLanguages[$localeCode])){
				$session->language = $availableLanguages[$localeCode];
			} else {
				$defaultLanguage = Solvex::config('locale/main/language_front');
				if (array_search($defaultLanguage, $availableLanguages)) {
                    $session->language = $defaultLanguage;
                } else {
                    $session->language = current($availableLanguages);
                }
			}
		}
		
		self::setTimezone();
	}
	
	private static function _getLocaleFromLanguageCode($code)
	{
		$localeList = self::getLocaleList(true);
		
		foreach($localeList as $locale){
			if(strstr($locale, $code)){
				return $locale;
			}
		}
		
		return self::DEFAULT_LOCALE;
	}
	
	public static function getLocale()
	{
		
	}
	
	public static function getDefaultLocale()
	{
		
	}
	
	public static function getLocaleList($installedOnly = false)
	{
		
	}
	
	public static function getInstallLocaleList()
	{
		
	}
	
	public static function getLanguageId()
	{
		
	}
	
	public static function getLanguageUrl()
	{
		
	}
	
	public static function getDefaultTimezone()
	{
		
	}
	
	public static function getTimezone()
	{
		
	}
	
	public static function setTimezone($timezone = null)
	{
		
	}
	
	public static function getAdminLocales()
	{
		
	}
	
	public static function getAvailableLocales()
	{
		
	}
	
	public static function getTimezoneList()
	{
		
	}
	
	public static function getCurrencyList()
	{
		
	}
}